<?php
namespace app\index\controller;
use http\Httplib;
use think\Db;
// use think\Exception;
class Index
{
    public function index()
    {
        /*$result = Db::query('update user set age=age+10 where uid=1');
        $res    = Db::query('update user set age=age-10 where uid=3');*/
        // Db::name('user')->where(['uid'=>1])->setInc('age',10);
        // Db::name('user')->where(['uid'=>1])->setDec('age',10);
        // Db::name('user')->where(['uid'=>3])->setDec('age',10);
        // Db::name('user')->where(['uid'=>3])->setInc('age',10);
        Db::startTrans();
        try {
            $result = Db::name('user')->where(['uid'=>1])->setDec('age',10);
            $res    = Db::name('user')->where(['uid'=>3])->setInc('age',10);
            // Db::name('user')->where(['uid'=>1])->update(['age'])
            if($result && $res){
                Db::commit();
            } else {
                echo '数据异常';
                Db::rollback();
                // printf('request:',$e->getCode,$e->getMessage());
                // throw new \Exception('ERROR');
            }
        } catch (Exception $e) {
            Db::rollback();
            printf('request:',$e->getCode,$e->getMessage());
            // return $e->getData();
        }

        /*return '<style type="text/css">*{ padding: 0; margin: 0; } .think_default_text{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:)</h1><p> ThinkPHP V5<br/><span style="font-size:30px">十年磨一剑 - 为API开发设计的高性能框架</span></p><span style="font-size:22px;">[ V5.0 版本由 <a href="http://www.qiniu.com" target="qiniu">七牛云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="ad_bd568ce7058a1091"></think>';*/
    }
    public  function uninstall()
    {
        Db::startTrans();
        try{
            $sql = init_sql($this->addons_path.'uninstall.sql');  //初始化sql
            foreach ($sql as $v){
                $r = Db::execute($v);
                if(!is_numeric($r)){
                    throw new TpshopException('ERROR',0,['status'=>0,'msg'=>'卸载时出现异常','result'=>$r]);
                }
            }
            Db::commit();
        }catch (TpshopException $e){
            Db::rollback();
            return $e->getErrorArr();
        }
        file_exists($this->addons_path.'install.lock') && unlink($this->addons_path.'install.lock');
        return ['status'=>1,'msg'=>'卸载成功','result'=>$r];
    }

    public function order_list(){
        $type = input('type');
        $search_key = input('search_key');
        $search_key = trim($search_key);// 搜索订单 根据商品名称 或者 订单编号
        $order = new OrderModel();
        $where_arr = [
            'user_id'=>$this->user_id,
            'deleted'=>0,//删除的订单不列出来
            'prom_type'=>['in',[0,4,9]]
        ];
        $count = $order->where($where_arr)
            ->where(function ($query) use ($type) {
                if ($type) {
                    $query->where("1=1".C(strtoupper($type)));
                }
            })
            ->where(function ($query) use ($search_key) {
                if ($search_key) {
                    $query->where('order_sn', 'like', $search_key)->whereOr('order_id', 'IN', function ($query) use ($search_key) {
                        $query->name('order_goods')->where('goods_name', 'like', $search_key)->field('order_id');
                    });
                }
            })
            ->count();
        $Page = new Page($count,10);

        $order_list = $order->where($where_arr)
            ->where(function ($query) use ($type) {
                if ($type) {
                    $query->where("1=1".C(strtoupper($type)));
                }
            })
            ->where(function ($query) use ($search_key) {
                if ($search_key) {
                    $query->where('order_sn', 'like', $search_key)->whereOr('order_id', 'IN', function ($query) use ($search_key) {
                        $query->name('order_goods')->where('goods_name', 'like', $search_key)->field('order_id');
                    });
                }
            })
            ->limit($Page->firstRow . ',' . $Page->listRows)->order("order_id DESC")->select();

        $this->assign('page',$Page);
        $this->assign('order_list',$order_list);
        return $this->fetch();
    }

    public function spider()
    {
        $http = new Httplib();
        $str = $http->request('');
    }

    public function test()
    {
        print_r(333);die;
    }
}
